// Copyright 2010 and onwards Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview This content script is loaded in the context of the Google
 * Calendar web app (http[s]://www.google.com/calendar) and replaces the
 * default favicon (the one with the number "31") with the current date.
 *
 * @author manas@google.com (Manas Tungare)
 */

/**
 * The base image needs to be a data URI because Chrome won't let us muck
 * around with images from the chrome-extension:// scheme being inserted
 * into an https:// page.
 */
var iconImage = '';

updateFavicon();

/**
 * Updates the favicon, and sets a timeout for calling itself one second
 * after midnight. This is needed for those who keep Calendar open in a tab
 * and never close it.
 */
function updateFavicon() {
  var now = new Date();
  paintIcon(iconImage, now.getDate());

  // Set a timeout to update the favicon 1 second after midnight.
  var midnight = new Date();
  midnight.setDate(now.getDate() + 1);
  midnight.setHours(0);
  midnight.setMinutes(0);
  midnight.setSeconds(1);
  setTimeout(updateFavicon, (midnight.getTime() - now.getTime()));
}


/**
 * Creates a canvas, paints a base image onto it, then overlays the current
 * date, and exports it as a PNG data URI. This data URI is then set as the
 * favicon of the page.
 *
 * Portions based on http://remysharp.com/2010/08/24/dynamic-favicons
 * Code licensed as Creative Commons BY-NC-SA. (Thanks, Remy!)
 *
 * @param {String} baseImage Filename of the base calendar icon image.
 * @param {Number} number The number to overlay on the base image.
 */
function paintIcon(baseImage, number) {
  var canvas = document.createElement('canvas'),
      img = document.createElement('img');

  canvas.height = canvas.width = 16;
  var context = canvas.getContext('2d');
  img.onload = function() {
    context.drawImage(this, 0, 0);
    context.font = 'bold 10px "Helvetica", sans-serif';
    context.fillStyle = '#fff';

    // We can't make tracking tighter via fillText, so do manual hacky
    // tracking. Default tracking is too loose for this use case.
    if (number < 10) {
      context.fillText(number, 5.5, 13);
    } else {
      context.fillText(Math.floor(number / 10), 3, 13);
      context.fillText(number % 10, 8, 13);
    }

    // Insert new favicon.
    var link = document.createElement('link');
    link.type = 'image/x-icon';
    link.rel = 'shortcut icon';
    link.href = canvas.toDataURL('image/png');
    document.body.appendChild(link);

    // Remove original favicon link.
    var oldLink = document.getElementById('favicon');
    oldLink.parent.removeChild(oldLink);
  };

  img.src = baseImage;
}
